package com.thams.dao.base;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.thams.util.pageUtil.core.PageSqlExecutor;
import com.thams.util.pageUtil.core.ReflectUtil;

public abstract class BaseDao extends SqlMapClientDaoSupport {   
	/**
	 * 数据库连接
	 * 
	 * @return SqlMapClientTemplate
	 */
	public SqlMapClientTemplate getConn() {
		// 获取模板
		return (SqlMapClientTemplate)this.getSqlMapClientTemplate();
	}
	/**
	 * 返回要查询所有记录数,建议只查询一次.
	 * @author: luyu
	 * @data: Sep 9, 2008
	 * @param statementName      
	 * @param parameterObject
	 */
	public Integer getQueryAllNum(String statementName , Object parameterObject){
		return getConn().queryForList(statementName, parameterObject).size();
	}
	
    /**
     * @author: luyu
     * @data: Sep 9, 2008
     * @throws Exception
     *
     * @function: 调用反射机制注入executor
     */
    public void initialize() throws Exception {   
        if (sqlExecutor != null) {   
            SqlMapClient sqlMapClient = getSqlMapClientTemplate()   
                    .getSqlMapClient();   
            if (sqlMapClient instanceof SqlMapClient) {   
                ReflectUtil.setFieldValue(((ExtendedSqlMapClient) sqlMapClient)   
                        .getDelegate(), "sqlExecutor", SqlExecutor.class,   
                        sqlExecutor);   
            }   
        }   
    }

    public void setSqlExecutor(SqlExecutor sqlExecutor) {   
        this.sqlExecutor = sqlExecutor;   
    }   
  
    public void setEnableLimit(boolean enableLimit) {   
        if (sqlExecutor instanceof PageSqlExecutor) {   
            ((PageSqlExecutor) sqlExecutor).setEnableLimit(enableLimit);   
        }   
    }
    
    public SqlExecutor getSqlExecutor() {   
        return sqlExecutor;   
    }   
    
    private SqlExecutor sqlExecutor;   
}  

